package com.masterclient.app.modules.firstpager.fragments.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.firstpager.BrandListBean;
import com.eoner.baselibrary.bean.firstpager.SearchVideoBean;
import com.eoner.baselibrary.bean.firstpager.VideoListBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 作者：sean
 * 创建时间：2017/7/3 09:05
 * 功能说明：
 */

public class VideoPresenter extends VideoContract.Presenter {
    @Override
    public void getVideoList(String brand_id, String page, String page_size, String sort) {
        Observable<VideoListBean> observable = RetrofitUtil.getInstance().getVideoList(NetEnvManager.BASE_SURL+"1/video/"+page+"-"+page_size+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<VideoListBean>() {
            @Override
            public void accept(@NonNull VideoListBean bean) throws Exception {
                if(bean!=null && bean.getData()!=null){
                    if("1".equals(page)){
                        mView.showVideoHead(bean.getData());
                        mView.showVideoList(bean.getData());
                    }else {
                        mView.showVideoList(bean.getData());
                    }
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }

    @Override
    public void getBrandList() {
        Observable<BrandListBean> observable = RetrofitUtil.getInstance().getBrandList(NetEnvManager.BASE_SURL+"1/brand/brand_list.html");
        Disposable disposable = toSubscribe(observable, new Consumer<BrandListBean>() {
            @Override
            public void accept(@NonNull BrandListBean bean) throws Exception {
                if(bean!=null && bean.getData()!=null){
                    mView.showBrandList(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void getSearchList(String kw, String page, String page_size) {
        Observable<SearchVideoBean> observable = RetrofitUtil.getInstance().videoSearch(NetEnvManager.BASE_SURL+"1/video/search/"+kw+"/"+page+"-"+page_size+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<SearchVideoBean>() {
            @Override
            public void accept(@NonNull SearchVideoBean bean) throws Exception {
                if(0==bean.getCode()){
                    mView.showSearchList(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}
